package com.lowca.news.entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Entity
@Table(name = "t_article_comment", catalog = "com_lowca_news")
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class ArticleComment extends IdEntity implements Serializable {

	private static final long serialVersionUID = 1L;

	private User publisher;
	private String publisherIp;
	private Date publishTime;
	private String subject;
	private String content;
	private User checker;
	private Integer sortValue;
	private Boolean checked = Boolean.TRUE;
	private Boolean deleted = Boolean.FALSE;
	private Article article;

	public ArticleComment() {
		super();
	}

	public ArticleComment(User publisher, String publisherIp, Date publishTime,
			String subject, String content, Boolean deleted, User checker,
			Integer sortValue, Boolean checked, Article article) {
		super();
		this.publisher = publisher;
		this.publisherIp = publisherIp;
		this.publishTime = publishTime;
		this.subject = subject;
		this.content = content;
		this.deleted = deleted;
		this.checker = checker;
		this.sortValue = sortValue;
		this.checked = checked;
		this.article = article;
	}

	@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = false)
	@JoinColumn(name = "publisher_id")
	public User getPublisher() {
		return publisher;
	}

	public void setPublisher(User publisher) {
		this.publisher = publisher;
	}

	@Column(name = "publisher_ip", length = 30)
	public String getPublisherIp() {
		return publisherIp;
	}

	public void setPublisherIp(String publisherIp) {
		this.publisherIp = publisherIp;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "publish_time", length = 19)
	public Date getPublishTime() {
		return publishTime;
	}

	public void setPublishTime(Date publishTime) {
		this.publishTime = publishTime;
	}

	@Column(name = "subject", length = 255)
	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	@Column(name = "content", length = 1000)
	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	@Column(name = "deleted")
	public Boolean getDeleted() {
		return deleted;
	}

	public void setDeleted(Boolean deleted) {
		this.deleted = deleted;
	}

	@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = false)
	@JoinColumn(name = "checker_id")
	public User getChecker() {
		return checker;
	}

	public void setChecker(User checker) {
		this.checker = checker;
	}

	@Column(name = "sort_value", length = 4)
	public Integer getSortValue() {
		return sortValue;
	}

	public void setSortValue(Integer sortValue) {
		this.sortValue = sortValue;
	}

	@Column(name = "checked")
	public Boolean getChecked() {
		return checked;
	}

	public void setChecked(Boolean checked) {
		this.checked = checked;
	}

	@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = false)
	@JoinColumn(name = "article_id")
	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

	public boolean equals(Object other) {
		if (this == other)
			return true;
		if (!(other instanceof Article))
			return false;
		final ArticleComment ac = (ArticleComment) other;
		if (!getSubject().equals(ac.getSubject()))
			return false;
		if (!getContent().equals(ac.getContent()))
			return false;
		if (getArticle() != null && !getArticle().equals(ac.getArticle()))
			return false;
		if (getPublisher() != null && !getPublisher().equals(ac.getPublisher()))
			return false;
		return true;
	}

	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + getSubject().hashCode();
		result = prime * result + getContent().hashCode();
		result = prime * result
				+ ((getArticle() == null) ? 0 : getArticle().hashCode());
		result = prime * result
				+ ((getPublisher() == null) ? 0 : getPublisher().hashCode());
		return result;
	}
}
